package com.ciban.smarthome.module.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciban.smarthome.R;
import com.ciban.smarthome.module.ui.base.BaseActivity;

public class PwdActivity extends BaseActivity implements OnClickListener {

	private TextView tv_fund, tv_close, tv_txt, tv_reset;
	private LinearLayout ll_has, ll_not;
	private String pwd;
	private int pwdType;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting_pwd);

		initData();

		initViews();
	}

	private void initData() {

		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(this);
		pwdType = sp.getInt("pwd_type", 0);
		if (pwdType > 0) {
			pwd = sp.getString("pwd_content", "");
		}

	}

	private void initViews() {
		ll_has = (LinearLayout) findViewById(R.id.pwd_setting_has);
		ll_not = (LinearLayout) findViewById(R.id.pwd_setting_not);
		tv_fund = (TextView) findViewById(R.id.pwd_setting_fund);
		tv_close = (TextView) findViewById(R.id.pwd_setting_close);
		tv_txt = (TextView) findViewById(R.id.pwd_setting_txt);
		tv_reset = (TextView) findViewById(R.id.pwd_setting_reset);

		tv_fund.setOnClickListener(this);
		tv_close.setOnClickListener(this);
		tv_txt.setOnClickListener(this);
		tv_reset.setOnClickListener(this);

		refreshView();
	}

	private void refreshView() {
		if (pwdType > 0) {
			ll_has.setVisibility(View.VISIBLE);
			ll_not.setVisibility(View.GONE);
		} else {
			ll_has.setVisibility(View.GONE);
			ll_not.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.pwd_setting_fund:
			intent = new Intent(PwdActivity.this, PwdFundActivity.class);
			startActivityForResult(intent, 100);
			break;
		case R.id.pwd_setting_txt:
			break;
		case R.id.pwd_setting_close:
			if (pwdType == 1) {

			} else if (pwdType == 2) {
				intent = new Intent(PwdActivity.this, PwdFundActivity.class);
				intent.putExtra("pwd", pwd);
				startActivityForResult(intent, 101);
			}

			break;
		case R.id.pwd_setting_reset:
			if (pwdType == 1) {

			} else if (pwdType == 2) {
				intent = new Intent(PwdActivity.this, PwdFundActivity.class);
				intent.putExtra("pwd", pwd);
				intent.putExtra("reset", true);
				startActivityForResult(intent, 102);
			}
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case 100:
			if (resultCode == RESULT_OK) {
				refreshPwdConfig(2, data.getStringExtra("pwd"));
			}
			break;
		case 101:
			if (resultCode == RESULT_OK) {
				refreshPwdConfig(0, "");
			}
			break;
		case 102:
			if (resultCode == RESULT_OK) {
				refreshPwdConfig(2, data.getStringExtra("pwd"));
			}
			break;
		}
	}

	private void refreshPwdConfig(int type, String pwd) {
		pwdType = type;
		this.pwd = pwd;
		refreshView();

		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(PwdActivity.this);
		Editor edit = sp.edit();
		edit.putInt("pwd_type", type);
		edit.putString("pwd_content", pwd);
		edit.commit();
	}

}
